package org.bovver.restful;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by Paul on 2014/11/27.
 */
@Controller("/api")
public class AllUrlController {
    @Autowired
    private RequestMappingHandlerMapping handlerMapping;
    @RequestMapping("/api")
    public String index(Long id,HttpServletRequest request, HttpServletResponse response) throws IOException, ClassNotFoundException {

        Map map =  this.handlerMapping.getHandlerMethods();
        Iterator<?> iterator = map.entrySet().iterator();
        Set<RequestMappingInfo> set = map.keySet();

        /*while(iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            System.out.println(entry.getKey() +"\n" + entry.getValue());
        }*/
        for (RequestMappingInfo info : set) {
            Set<String> patterns = info.getPatternsCondition().getPatterns();
            for (String pattern : patterns) {
                System.out.println(pattern);
            }
        }
        request.getServletContext().setAttribute("apis", set);
        System.out.println(set);
        return "redirect:api.jsp";
    }
}
